#include "ConversationBox.h"

ConversationBox::ConversationBox(TextureManager * textureManager, Vector2D position, Vector2D size, float textSize, GameManager * gameManager, void (GameManager::*clickAction)(void))
:MenuElement(textureManager, "", position, size, NULL, gameManager, clickAction)
{
	textBox = NULL;
	target = NULL;
	this->textSize = textSize;
	textBoxPosition = size * 0.05;
	textBoxSize = size * 0.9;
	speechLinePosition = Vector2D(0,0);
}
ConversationBox::~ConversationBox()
{
	if(textBox)
	{
		delete(textBox);
		textBox = NULL;
	}
}
void ConversationBox::SetText(Object * object, Camera * camera, char * newText)
{
	printf("setting text to %s\n", newText);
	if(textBox)
	{
		delete(textBox);
		textBox = NULL;
	}
	textBox = new TextBox(textureManager, newText, textBoxPosition, textBoxSize, textSize);
	target = object;
	this->camera = camera;
	size.y = textBox->GetNeededHeight() / 0.9;
	textBoxPosition = size * 0.05;
	textBox->SetPosition(textBoxPosition);
	speechLinePosition = camera->ConvertToCameraSpace(target->position + (target->vertices[0] + target->vertices[1])*0.5);
	
}
void ConversationBox::Draw(Vector2D parentPosition)
{
	if(!textBox)
		return;
		
	textBox->Draw(parentPosition + position);
	speechLinePosition = camera->ConvertToCameraSpace(target->position + (target->vertices[0] + target->vertices[1])*0.5);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,100,100,0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	
	
	glPushMatrix();
	glLoadIdentity();
	glColor3f(1,1,1);
	
	glBegin(GL_TRIANGLES);
	glVertex2f(size.x * 0.5 - size.x * 0.08 + parentPosition.x + position.x, size.y * 0.5 + parentPosition.y + position.y);
	glVertex2f(size.x * 0.5 + size.x * 0.08 + parentPosition.x + position.x, size.y * 0.5 + parentPosition.y + position.y);
	
	Vector2D endPoint = Vector2D(size.x * 0.5 + parentPosition.x + position.x, size.y * 0.5 + parentPosition.y + position.y);
	endPoint = (speechLinePosition - endPoint) * 0.1;
	endPoint = speechLinePosition - endPoint;
	//printf("speechLinePosition = (%f, %f)\n", speechLinePosition.x, speechLinePosition.y);
	//glTranslatef(-parentPosition.x-position.x, -parentPosition.y-position.y, 0);
	glVertex2f(endPoint.x, endPoint.y);
	glEnd();
	
	glTranslatef(parentPosition.x+position.x, parentPosition.y+position.y, 0);

	glColor3f(0,0,0);
	glDisable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	
	glVertex2f(0,0);
	glVertex2f(size.x, 0);
	glVertex2f(size.x, size.y);
	glVertex2f(0, size.y);
	
	glEnd();
	
	glPushMatrix();
	glLoadIdentity();
	
	
	glColor3f(0,0,0);
	glBegin(GL_TRIANGLES);
	glVertex2f(size.x * 0.5 - size.x * 0.1 + parentPosition.x + position.x, size.y * 0.5 + parentPosition.y + position.y);
	glVertex2f(size.x * 0.5 + size.x * 0.1 + parentPosition.x + position.x, size.y * 0.5 + parentPosition.y + position.y);
	//Vector2D speechPosition = speechLinePosition - parentPosition - position;
	//printf("speechLinePosition = (%f, %f)\n", speechLinePosition.x, speechLinePosition.y);
	//glTranslatef(-parentPosition.x-position.x, -parentPosition.y-position.y, 0);
	glVertex2f(speechLinePosition.x, speechLinePosition.y);
	glEnd();
	
	

	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}
